package woodnaonly.radiobutto_test.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import java.util.ArrayList;

import me.nereo.multi_image_selector.MultiImageSelectorActivity;
import woodnaonly.radiobutto_test.Adapter.GridAdapter;
import woodnaonly.radiobutto_test.ImagePagerActivity;
import woodnaonly.radiobutto_test.R;

/**
 * Created by woodnaonly on 2015/10/29.
 */
public class MultiImageSelectorFagment extends Fragment implements View.OnClickListener
{
    private static final int REQUEST_IMAGE = 2;
    private static final int RESULT_OK = -1;
    private Button mButton;
    private ArrayList<String> mSelectPath;
    private GridView mGridView;
    private TextView mResultText;
    private GridAdapter mGridAdapter;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE)
        {
            if (resultCode == RESULT_OK)
            {

                mSelectPath = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
                StringBuilder sb = new StringBuilder();
                for (String p : mSelectPath)
                {
                    sb.append(p);
                    sb.append("\n");
                    mResultText.setText(sb.toString());
                }
                mGridAdapter = new GridAdapter(getContext(), mSelectPath);
                mGridView.setAdapter(mGridAdapter);
                setitemsize();
                mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
                {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
                    {
                        Intent intent = new Intent(getActivity(), ImagePagerActivity.class);
                        intent.putExtra(ImagePagerActivity.LIST, mSelectPath);
                        intent.putExtra(ImagePagerActivity.POSITION, position);
                        startActivity(intent);

                    }
                });
            }

        }

    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState)
    {
        return inflater.inflate(R.layout.layout1, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        mSelectPath = new ArrayList<String>();
        mButton = (Button) view.findViewById(R.id.layout1_button);
        mGridView = (GridView) view.findViewById(R.id.gridView);
        mResultText = (TextView) view.findViewById(R.id.result);
        mButton.setOnClickListener(this);

    }

    @Override
    public void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
    }


    @Override

    public void onClick(View v)
    {

        Intent intent = new Intent(getActivity(), MultiImageSelectorActivity.class);
        // 是否显示拍摄图片
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
        // 最大可选择图片数量
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 100);
        // 选择模式
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_MULTI);
        // 默认选择
        if (mSelectPath != null && mSelectPath.size() > 0)
        {
            intent.putExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, mSelectPath);
        }
        startActivityForResult(intent, REQUEST_IMAGE);
    }

    /**
     * 设置girdview宽高一样
     */

    void setitemsize()
    {

        final int desireSize = getResources().getDimensionPixelOffset(me.nereo.multi_image_selector.R.dimen.image_size);
        final int numCount = mGridView.getWidth() / desireSize;
        final int columnSpace = getResources().getDimensionPixelOffset(me.nereo.multi_image_selector.R.dimen.space_size);
        int columnWidth = (mGridView.getWidth() - columnSpace * (numCount - 1)) / numCount;
        mGridAdapter.setItemSize(columnWidth);
    }

}
